package com.jqsoft.launcher.activity.task;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jqsoft.launcher.activity.JQActivity;
import com.jqsoft.launcher.activity.bean.UserLoginInfo;
import com.jqsoft.launcher.common.BphsURL;
import com.jqsoft.launcher.common.Constant;
import com.jqsoft.launcher.R;
import com.jqsoft.launcher.util.MessageTools;
import com.jqsoft.launcher.util.WebServiceUtils;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class TaskListActivity extends JQActivity implements OnClickListener{

	private Button geren_btn,bendanwei_btn,shuaxin_btn;
	private TextView worktype_tv,gaoxueya_geren_tv,tangniaobing_geren_tv,jingshenbing_geren_tv,gaoxueya_danwei_tv,tangniaobing_danwei_tv,jingshenbing_danwei_tv;
	private LinearLayout geren_work_ll,danwei_work_ll,
	gaoxueya_geren_ll,tangniaobing_geren_ll,jingshenbing_geren_ll,gaoxueya_danwei_ll,tangniaobing_danwei_ll,jingshenbing_danwei_ll;
	private String dataType="1";//1个人   2工作   默认个人 
//	请求的结果
	private String sResult;
	private String sMessage;
	
	private String gaoxueya_gr_num,tangniaobing_gr_num,jingshenbing_gr_num,gaoxueya_gr_ids,tangniaobing_gr_ids,jingshenbing_gr_ids,
	gaoxueya_dw_num,tangniaobing_dw_num,jingshenbing_dw_num,gaoxueya_dw_ids,tangniaobing_dw_ids,jingshenbing_dw_ids;
	@Override
	protected int getContentLayout() {
		
		return R.layout.tasklist_main;
	}

	@Override
	protected void initGui() {
		geren_btn = (Button) findViewById(R.id.geren_btn);
		geren_btn.setOnClickListener(TaskListActivity.this);
		bendanwei_btn = (Button) findViewById(R.id.bendanwei_btn);
		bendanwei_btn.setOnClickListener(TaskListActivity.this);
		shuaxin_btn = (Button) findViewById(R.id.shuaxin_btn);
		shuaxin_btn.setOnClickListener(TaskListActivity.this);
		
		worktype_tv = (TextView) findViewById(R.id.worktype_tv);
		gaoxueya_geren_tv = (TextView) findViewById(R.id.gaoxueya_geren_tv);
		tangniaobing_geren_tv = (TextView) findViewById(R.id.tangniaobing_geren_tv);
		jingshenbing_geren_tv = (TextView) findViewById(R.id.jingshenbing_geren_tv);
		gaoxueya_danwei_tv = (TextView) findViewById(R.id.gaoxueya_danwei_tv);
		tangniaobing_danwei_tv = (TextView) findViewById(R.id.tangniaobing_danwei_tv);
		jingshenbing_danwei_tv = (TextView) findViewById(R.id.jingshenbing_danwei_tv);
		
		geren_work_ll = (LinearLayout) findViewById(R.id.geren_work_ll);
		danwei_work_ll = (LinearLayout) findViewById(R.id.danwei_work_ll);
		gaoxueya_geren_ll = (LinearLayout) findViewById(R.id.gaoxueya_geren_ll);
		tangniaobing_geren_ll = (LinearLayout) findViewById(R.id.tangniaobing_geren_ll);
		jingshenbing_geren_ll = (LinearLayout) findViewById(R.id.jingshenbing_geren_ll);
		gaoxueya_danwei_ll = (LinearLayout) findViewById(R.id.gaoxueya_danwei_ll);
		tangniaobing_danwei_ll = (LinearLayout) findViewById(R.id.tangniaobing_danwei_ll);
		jingshenbing_danwei_ll = (LinearLayout) findViewById(R.id.jingshenbing_danwei_ll);
		
		gaoxueya_geren_ll.setOnClickListener(TaskListActivity.this);
		tangniaobing_geren_ll.setOnClickListener(TaskListActivity.this);
		jingshenbing_geren_ll.setOnClickListener(TaskListActivity.this);
		gaoxueya_danwei_ll.setOnClickListener(TaskListActivity.this);
		tangniaobing_danwei_ll.setOnClickListener(TaskListActivity.this);
		jingshenbing_danwei_ll.setOnClickListener(TaskListActivity.this);
	}

	@Override
	protected void initAction() {
		setGoBack();

	}

	@Override
	protected void initData() {
		setTitle("工作提醒");
		requstMethod();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.geren_btn:// 个人工作
			dataType = "1";
			worktype_tv.setText("（个人工作）");
			geren_work_ll.setVisibility(View.VISIBLE);
			danwei_work_ll.setVisibility(View.GONE);
			return;
		case R.id.bendanwei_btn:// 本单位工作
			dataType = "2";
			worktype_tv.setText("（本单位工作）");
			geren_work_ll.setVisibility(View.GONE);
			danwei_work_ll.setVisibility(View.VISIBLE);
			break;
		case R.id.shuaxin_btn:// 刷新
			requstMethod();
			return;
			
		case R.id.gaoxueya_geren_ll:// 
			 toTaskdetailActivity("1", "1",   gaoxueya_gr_num,"高血压患者随访服务记录（个人）");
			 
			return;
		case R.id.tangniaobing_geren_ll:// 
			toTaskdetailActivity("1", "2",   tangniaobing_gr_num,"糖尿病患者随访服务记录（个人）");
			break;
		case R.id.jingshenbing_geren_ll:// 
			toTaskdetailActivity("1",  "3", jingshenbing_gr_num, "精神病患者随访服务记录（个人）");
			return;
		case R.id.gaoxueya_danwei_ll:// 
			toTaskdetailActivity("2", "1",  gaoxueya_dw_num, "高血压患者随访服务记录（本单位）");
			return;
		case R.id.tangniaobing_danwei_ll://
			toTaskdetailActivity("2", "2",   tangniaobing_dw_num,"糖尿病患者随访服务记录（本单位）");
			break;
		case R.id.jingshenbing_danwei_ll:// 
			toTaskdetailActivity("2", "3",  jingshenbing_dw_num, "精神病患者随访服务记录（本单位）");
			return;
			}
	}

	private void toTaskdetailActivity(String dataType,String pageNo,String allcount,String title) {
		Intent intent = new Intent(act,
				TaskdetailActivity.class);
		Bundle bundle = new Bundle();
		bundle.putString("dataType", dataType);
		bundle.putString("pageNo",pageNo);
	//	bundle.putString("ID",ID);
		bundle.putString("allcount",allcount);
		bundle.putString("title",title);
		intent.putExtras(bundle);
		startActivity(intent);
	}
	
	private final Handler showDownLoadHandle = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Constant.MSG_LOAD_OVER:
				dismissrogressBar();
				if ("1".equals(sResult)) {
					gaoxueya_geren_tv.setText(gaoxueya_gr_num);
					tangniaobing_geren_tv.setText(tangniaobing_gr_num);
					jingshenbing_geren_tv.setText(jingshenbing_gr_num);
					gaoxueya_danwei_tv.setText(gaoxueya_dw_num);
					tangniaobing_danwei_tv.setText(tangniaobing_dw_num);
					jingshenbing_danwei_tv.setText(jingshenbing_dw_num);
				} else {
					MessageTools.showDialogOk(ctx, sMessage);
				}
				break;
			case Constant.MSG_LOAD_ERROR:
				dismissrogressBar();
				MessageTools.showDialogOk(ctx, "请求失败！");
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	
	public void requstMethod() {
		UserLoginInfo loginInfo = UserLoginInfo.getInstances();
		
		loadWaitPanel();
			final JSONObject requestdata = new JSONObject();
			try {
				requestdata.put("sLoginName", loginInfo.getsLoginName());
				requestdata.put("sInputDeptCode", loginInfo.getsOrganizationKey() );
				requestdata.put("sAreaCode", loginInfo.getsManagementDivisionCode());
				System.out.println(requestdata.toString());
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
			new Thread() {
				public void run() {
					String serviceURL = BphsURL.getCommonPath("GongzuoRemind_URL");
					 String methodName= BphsURL.getCommonPath("GongzuoRemind_METHOD");
//					String methodName = "GetTiXing";
//					 String serviceURL =
//					 "http://192.168.44.163:9099/WebService/RemindWebService.asmx";
				String	result = WebServiceUtils.requestByParams(methodName,requestdata.toString(), serviceURL);
					// {"sSuccess":"1","sMessage":"用户登陆成功！","AllCount":0,"Data":"{\"sResult\":null,\"sMessage\":null,\"sLoginName\":\"admin\",\"sAlias\":null,\"sUserName\":\"安徽省卫生厅\",\"sOrganizationKey\":\"60bfbec0-180e-41b9-9974-007ef0d0ee43\",\"sOrgInstitutionCode\":\"X19870226\",\"sOrganizationName\":\"安徽省卫生厅\",\"sOrganizationTypeCode\":\"10\",\"sOrganizationLevelCode\":\"1\",\"sManagementDivisionCode\":\"340000\",\"sManagementDivisionName\":\"安徽省\"}"}
//				{"sSuccess":"1","sMessage":"执行功能！","AllCount":6,"Data":"{\"Table\":[{\"dataType\":\"0\",\"pageNo\":\"1\",\"CountNo\":\"2\",\"ID\":\"140707000027,140829000002\"},{\"dataType\":\"1\",\"pageNo\":\"1\",\"CountNo\":\"2\",\"ID\":\"140707000027,140829000002\"},{\"dataType\":\"0\",\"pageNo\":\"2\",\"CountNo\":\"12\",\"ID\":\"131122000020,131122000021,131122000022,131122000023,140327000001,131108000018,140327000004,110815000005,110908000002,130603000001,130603000003,130603000007,130603000008,130603000009,130603000010,140327000010,131108000017,140327000003,131121000001,131121000002,131121000003,131121000004,140327000002,130606000014,130606000015,130606000016,130606000017,130807000001,140327000005,140327000006,140327000007,140327000008,140327000009,131118000001,140530000001,140530000002,140829000001\"},{\"dataType\":\"1\",\"pageNo\":\"2\",\"CountNo\":\"12\",\"ID\":\"131122000020,131122000021,131122000022,131122000023,140327000001,131108000018,140327000004,110815000005,110908000002,130603000001,130603000003,130603000007,130603000008,130603000009,130603000010,140327000010,131108000017,140327000003,131121000001,131121000002,131121000003,131121000004,140327000002,130606000014,130606000015,130606000016,130606000017,130807000001,140327000005,140327000006,140327000007,140327000008,140327000009,131118000001,140530000001,140530000002,140829000001\"},{\"dataType\":\"0\",\"pageNo\":\"3\",\"CountNo\":\"2\",\"ID\":\"140505000001,101229001073,140430000002,131108000021\"},{\"dataType\":\"1\",\"pageNo\":\"3\",\"CountNo\":\"2\",\"ID\":\"140505000001,101229001073,140430000002,131108000021\"}]}"}	
				parseResultJson(result);
				}
			}.start();
		}
	private void parseResultJson(String result) {
		if (StringUtils.isNotBlank(result)) {

			JSONObject json;
			try {
				json = new JSONObject(result);
				sResult = json.getString("sSuccess");
				sMessage = json.getString("sMessage");
				String data = json.getString("Data");
				
				JSONArray jsonArray1 = new JSONArray(new JSONObject(data).getString("Table"));
				if ("0".equals(sResult)) {
					showDownLoadHandle.sendEmptyMessage(Constant.MSG_LOAD_OVER);
				} else {

					for (int i = 0; i < jsonArray1.length(); ++i) {
						JSONObject jsonData = (JSONObject) jsonArray1.get(i);
						String pageNo=jsonData.getString("pageNo");
						String dwCount=jsonData.getString("dwCount");//单位
						String grCount=jsonData.getString("grCount");//个人
						if(pageNo.equals("1")){ //1 高血压 2 糖尿病 3 精神病
							gaoxueya_gr_num = grCount;
							gaoxueya_dw_num = dwCount;
						}else if(pageNo.equals("2")){
							tangniaobing_gr_num = grCount;
							tangniaobing_dw_num = dwCount;
						}else{
							jingshenbing_gr_num= grCount;
							jingshenbing_dw_num = dwCount;
							 
						}
//						String dataType=jsonData.getString("dataType");
//						String pageNo=jsonData.getString("pageNo");
//						String CountNo=jsonData.getString("CountNo");
//						String ID=jsonData.getString("ID");
//						 "pageNo": "1",
//				            "dwCount": "0",
//				            "grCount": "0"
//						if("0".equals(dataType)){
//							if("1".equals(pageNo)){
//								gaoxueya_gr_num=CountNo;
//								gaoxueya_gr_ids=ID;
//							}
//							else if("2".equals(pageNo)){
//								tangniaobing_gr_num=CountNo;
//								tangniaobing_gr_ids=ID;
//							}
//							else if("3".equals(pageNo)){
//								jingshenbing_gr_num=CountNo;
//								jingshenbing_gr_ids=ID;
//							}
//						}else if("1".equals(dataType)){
//							if("1".equals(pageNo)){
//								gaoxueya_dw_num=CountNo;
//								gaoxueya_dw_ids=ID;
//							}
//							else if("2".equals(pageNo)){
//								tangniaobing_dw_num=CountNo;
//								tangniaobing_dw_ids=ID;
//							}
//							else if("3".equals(pageNo)){
//								jingshenbing_dw_num=CountNo;
//								jingshenbing_dw_ids=ID;
//							}
//						}

					}
					showDownLoadHandle.sendEmptyMessage(Constant.MSG_LOAD_OVER);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} else {
			showDownLoadHandle.sendEmptyMessage(Constant.MSG_LOAD_ERROR);
		}
	};
}


